package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import cn.com.ghy.android.util.XMLStatic;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天协访经销商信息解析器
 * @author wuz  @date 2011-6-21 16:29:16
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CoopVisitDealerInfoBeanParser {
	
	public static String createVsPartDataXMLForDealer(String vstype, CoopVisitDealerInfoBean bean,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        
        xmlSerializer.startTag("", XMLStatic.VSTYPE);
        xmlSerializer.text(vstype);
		xmlSerializer.endTag("", XMLStatic.VSTYPE);
        xmlSerializer.startTag("", XMLStatic.KUNNR);
        xmlSerializer.text(bean.getKunnr());
		xmlSerializer.endTag("", XMLStatic.KUNNR);
		xmlSerializer.startTag("", XMLStatic.VSDATE);
        xmlSerializer.text(bean.getVsdate());
        xmlSerializer.endTag("", XMLStatic.VSDATE);
    	xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.text(bean.getContent());
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.startTag("", XMLStatic.QUESTION);
        xmlSerializer.text(bean.getQuestion());
		xmlSerializer.endTag("", XMLStatic.QUESTION);
		xmlSerializer.startTag("", XMLStatic.IMPROVE);
        xmlSerializer.text(bean.getImprove());
		xmlSerializer.endTag("", XMLStatic.IMPROVE);
		xmlSerializer.startTag("", XMLStatic.THREEAVGMONTHSALE);
        xmlSerializer.text(bean.getThreeavgmonthsale());
		xmlSerializer.endTag("", XMLStatic.THREEAVGMONTHSALE);
		xmlSerializer.startTag("", XMLStatic.MONTHTARSALE);
        xmlSerializer.text(bean.getMonthtarsale());
		xmlSerializer.endTag("", XMLStatic.MONTHTARSALE);
		xmlSerializer.startTag("", XMLStatic.TONOWSALE);
        xmlSerializer.text(bean.getTonowsale());
		xmlSerializer.endTag("", XMLStatic.TONOWSALE);
		xmlSerializer.startTag("", XMLStatic.TONOWREACH);
        xmlSerializer.text(bean.getTonowreach());
		xmlSerializer.endTag("", XMLStatic.TONOWREACH);
		xmlSerializer.startTag("", XMLStatic.LASTSTOCKNUM);
        xmlSerializer.text(bean.getLaststocknum());
		xmlSerializer.endTag("", XMLStatic.LASTSTOCKNUM);
		xmlSerializer.startTag("", XMLStatic.MONTHZDAYS);
        xmlSerializer.text(bean.getMonthzdays());
		xmlSerializer.endTag("", XMLStatic.MONTHZDAYS);
		xmlSerializer.startTag("", XMLStatic.WEEKSTOCKNUM);
        xmlSerializer.text(bean.getWeekstocknum());
		xmlSerializer.endTag("", XMLStatic.WEEKSTOCKNUM);
		xmlSerializer.startTag("", XMLStatic.CUSER);
        xmlSerializer.text(bean.getCuser());
		xmlSerializer.endTag("", XMLStatic.CUSER);
		xmlSerializer.startTag("", XMLStatic.ISVISIT);
        xmlSerializer.text("");
		xmlSerializer.endTag("", XMLStatic.ISVISIT);
		
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
	
	public static List<CoopVisitDealerInfoBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        CoopVisitDealerInfoBean bean = null;
        List<CoopVisitDealerInfoBean> list = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<CoopVisitDealerInfoBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table".equals(parser.getName())) {
                	bean = new CoopVisitDealerInfoBean();
                } else if (null != list && null != bean) {
                    if ("KUNNR".equals(parser.getName())) {   
                    	bean.setKunnr(parser.nextText());   
                    } else if("VSDATE".equals(parser.getName())) {
                    	bean.setVsdate(parser.nextText());
                    } else if("CONTENT".equals(parser.getName())) {
                    	bean.setContent(parser.nextText());
                    } else if("QUESTION".equals(parser.getName())) {
                    	bean.setQuestion(parser.nextText());
                    } else if("IMPROVE".equals(parser.getName())) {
                    	bean.setImprove(parser.nextText());
                    } else if("THREEAVGMONTHSALE".equals(parser.getName())) {
                    	bean.setThreeavgmonthsale(parser.nextText());
                    } else if("MONTHTARSALE".equals(parser.getName())) {
                    	bean.setMonthtarsale(parser.nextText());
                    } else if("TONOWSALE".equals(parser.getName())) {
                    	bean.setTonowsale(parser.nextText());
                    } else if("TONOWREACH".equals(parser.getName())) {
                    	bean.setTonowreach(parser.nextText());
                    } else if("LASTSTOCKNUM".equals(parser.getName())) {
                    	bean.setLaststocknum(parser.nextText());
                    } else if("MONTHZDAYS".equals(parser.getName())) {
                    	bean.setMonthzdays(parser.nextText());
                    } else if("WEEKSTOCKNUM".equals(parser.getName())) {
                    	bean.setWeekstocknum(parser.nextText());
                    } else if("CUSER".equals(parser.getName())) {
                    	bean.setCuser(parser.nextText());
                    } else if("CDATE".equals(parser.getName())) {
                    	bean.setCdate(parser.nextText());
                    } else if("ISVISIT".equals(parser.getName())) {
                    	bean.setIsvisit(parser.nextText());
                    } else if("ENTERTIME".equals(parser.getName())) {
                    	bean.setEntertime(parser.nextText());
                    } else if("LEAVETIME".equals(parser.getName())) {
                    	bean.setLeavetime(parser.nextText());
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table".equals(parser.getName())) {
                	if (null != list && bean != null) {
                		list.add(bean);
                		bean = null;
                	} 
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
}
